<template>

  <div>
    <nav class="navbar navbar-default">
      <div class="container-fluid">
        <!-- Brand and toggle get grouped for better mobile display -->
        <div class="navbar-header">

          <button type="button" class="navbar-toggle collapsed" data-toggle="collapse"
                  data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
            <span class="sr-only">Toggle navigation</span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
          </button>
          <a class="navbar-brand" href="#"><img :src="img" height="25px" width="25px"/></a>
        </div>

        <!-- Collect the nav links, forms, and other content for toggling -->
        <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
          <ul class="nav navbar-nav">
            <li
              v-for="item in navdata"
              :class="{active:isActive(item)}"
              :key="navdata.indexOf(item)">
              <router-link :to="item.route">{{item.title}}</router-link>
            </li>
          </ul>
          <form class="navbar-form navbar-left">
            <div class="form-group">
              <input type="text" v-model="sear" class="form-control" placeholder="搜索内容"
                     @keyup.native.enter="handleKeyup">
            </div>
            <router-link style="text-decoration: none" :to="{ path: 'search', query: { keywords: this.sear }}">
              <button class="btn" type="button">搜索</button>
            </router-link>
          </form>
          <ul class="nav navbar-nav navbar-right">
            <li class="dropdown" v-if="!loginStatus">
              <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true"
                 aria-expanded="false">登录<span class="caret"></span></a>
              <ul class="dropdown-menu">
                <form class="form-inline navbar-form">
                  <div class="form-group">
                    <label for="exampleInputName2">用户名</label>
                    <input v-model="username" type="text" class="form-control" id="exampleInputName2" placeholder="用户名">
                  </div>
                  <div class="form-group">
                    <label for="exampleInputEmail2">密码</label>
                    <input type="password" v-model="password" class="form-control" id="exampleInputEmail2"
                           placeholder="密码">
                  </div>
                  <button type="button" class="btn btn-default" @click="submitLogin">登录</button>
                </form>
              </ul>
            </li>
            <li v-if="!loginStatus">
              <router-link to="/msignin">注册</router-link>
            </li>
            <li v-if="loginStatus">
              <img src=" "/>
            </li>
            <li class="dropdown" v-if="loginStatus">

              <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true"
                 aria-expanded="false">欢迎,{{logSuccess.nickname}} !</a>
              <ul class="dropdown-menu">
                <li>
                  <router-link to="">
                    账号设置
                  </router-link>
                </li>
                <li>
                  <a @click="exit" style="cursor: pointer">退出登录</a>
                </li>
              </ul>
            </li>
          </ul>

        </div><!-- /.navbar-collapse -->
      </div><!-- /.container-fluid -->
    </nav>

  </div>
</template>

<script>
  import search_result from '../request/req';
  import signin from '../pages/sign_in'
  import axios from 'axios';

  const md5 = require('../util/md5');
  export default {
    name: "nav_bar",
    components: {
      signin
    },
    mounted() {
      axios.get('http://localhost:3000/login', {withCredentials: true}).then(res => {
        if (res.data.code == 1) {
          this.loginStatus = 1;
          this.logSuccess.nickname = res.data.nickname;
        }
        // console.log(res.data);
      }).catch(err => {
        console.log(err)
      });
    },
    data() {
      return {
        loginStatus: 0,
        dialogFormVisible: false,
        sear: '',
        username: '',
        logSuccess: {},
        password: '',
        img: require('../assets/logo.png'),
        navdata: [
          {title: '音乐首页', route: '/'},
          {title: '热门', route: '/hot'},
          {title: 'JJ', route: '/jj'}],
        isPhone: navigator.userAgent.indexOf('iPhone') != -1 || navigator.userAgent.indexOf('Android') != -1
      }

    }, methods: {
      exit() {
        this.loginStatus = 0;
        axios.get('http://localhost:3000/login/exit').then(res => {
          this.$message({
            message: res.data.msg,
            type: 'success'
          });
        })
      },
      isActive(i) {
        return this.$route.path === i.route;
      },
      search() {
        console.log(this.$refs.search.value);
        axios.get('/api/search?keywords=' + this.$refs.search.value)
          .then(res => {
            // console.log(res);
          })
      },
      handleKeyup() {
        this.$router.push({path: 'search', query: {keywords: this.sear}});
      },
      submitLogin() {
        axios.post('http://localhost:3000/login', {
          username: this.username,
          password: md5(this.password)
        }, {withCredentials: true}).then(res => {
          let c = res.data.code
          if (c == 0) {
            this.$message({
              message: res.data.msg,
              type: 'error'
            });
          }
          if (c == 1) {
            this.loginStatus = 1;
            // this.logSuccess.username=res.data.username;
            this.logSuccess.nickname = res.data.nickname;
            console.log(this.logSuccess.nickname, res.data.nickname);
            this.$message({
              message: res.data.msg,
              type: 'success'
            });
          }
          if (c == 2) {
            this.$message({
              message: res.data.msg,
              type: 'error'
            });
          }

        }).catch(error => {
          console.log(err)
        })
      }
    }
  }
</script>

<style scoped>

</style>
